//
//  DirectionMenubarView.m
//  testModel
//
//  Created by 刘晓萌 on 15/12/26.
//  Copyright © 2015年 刘晓萌. All rights reserved.


#import "DirectionMenubarView.h"

#define DRWIDTH  (self.frame.size.width)
#define DRHEIGHT  (self.frame.size.height)
#define ELEMENTWIDTH ((NSInteger) (DRWIDTH/(self.titleArr.count)))

@interface DirectionMenubarView ()

@property (strong,nonatomic) UIImageView *pointer;
@property (assign, nonatomic) NSInteger selectedTag;

@end

@implementation DirectionMenubarView

+(instancetype)instntceWithFrame:(CGRect)frame andTitleArr:(NSMutableArray *)titleArr{
    DirectionMenubarView *directionMenubarView = [[DirectionMenubarView alloc]initWithFrame:frame];
    directionMenubarView.titleArr = titleArr;
    [directionMenubarView makeTitleLabel];
    [directionMenubarView addSubview:directionMenubarView.backGroundImageView];
    [directionMenubarView sendSubviewToBack:directionMenubarView.backGroundImageView];
    return  directionMenubarView;
}
#pragma mark --- 功能实现
-(void)makeTitleLabel{
    for (int i = 0; i < self.titleArr.count; i ++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i * ELEMENTWIDTH,0,ELEMENTWIDTH,DRHEIGHT - 10)];
        label.text = self.titleArr[i];
        label.font = self.font;
        label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:label];
        label.userInteractionEnabled = YES;
        if (i == 0) {
            label.tag = self.startTag;
            self.selectedTag = self.startTag;
            label.textColor = self.selectedColor;
        }else{
            label.tag = self.startTag - i;
            label.textColor = self.normalColor;
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [label addGestureRecognizer:tap];
    }
    if (self.titleArr.count) {
        [self addSubview:self.pointer];
    }
}

/**
 * 点击事件(doChang可活动添加需要变化的其他页)
 */
-(void)tapAction:(UITapGestureRecognizer *)sender{
    UILabel *tapedLabel = (UILabel *)(sender.view);
    [self changTitle:tapedLabel];
    if (self.doChang) {
        self.doChang();
    }
}

/**
 * 修改选择的title
 */
-(void)changTitle:(UILabel *)tapedLabel{
    UILabel *prevLabel = (UILabel *)[self viewWithTag:self.selectedTag];
    if (prevLabel) {
        prevLabel.textColor = self.normalColor;
    }
    self.selectedTag = tapedLabel.tag;
    tapedLabel.textColor = self.selectedColor;
    [self changPointer:tapedLabel.frame.origin.x];
}

/**
 * 修改pointer的位置
 */
-(void)changPointer:(CGFloat)x{
    [UIView animateWithDuration:0.3 animations:^{
        _pointer.frame = CGRectMake(x+(ELEMENTWIDTH/2-8),DRHEIGHT-10,18,10);
    }];
}

#pragma mark --- .h方法,均可调用
/**
 * 根据下标变化title
 */
-(void)changTitleWithIndex:(NSInteger)index{
    UILabel *tapedLabel = (UILabel *)[self viewWithTag:(self.startTag - index)];
    if (tapedLabel) {
        [self changTitle:tapedLabel];
    }
}

/**
 * 得到被点击title的序号,从0开始
 */
-(NSInteger)getTapedIndex{
    return (([self viewWithTag:self.selectedTag].frame.origin.x)/ELEMENTWIDTH);
}

#pragma mark --- 懒加载/set方法重写

-(UIImageView *)pointer{
   if (!_pointer) {
        _pointer = [[UIImageView alloc]initWithFrame:CGRectMake(ELEMENTWIDTH/2-8, DRHEIGHT-10,18, 10)];
        _pointer.image = [UIImage imageNamed:@"pointer"];
    }
    return _pointer;
}

-(NSMutableArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}

-(UIColor *)selectedColor{
    if (!_selectedColor) {
        _selectedColor = [UIColor whiteColor];
    }
    return _selectedColor;
}

-(UIColor *)normalColor{
    if (!_normalColor) {
        _normalColor = [UIColor lightGrayColor];
    }
    return _normalColor;
}

-(NSInteger)startTag{
    if (_startTag == 0) {
        _startTag = 999;
    }
    return _startTag;
}

-(UIImageView *)backGroundImageView{
    if (!_backGroundImageView) {
        _backGroundImageView = [[UIImageView alloc]initWithFrame:self.bounds];
        _backGroundImageView.image = [UIImage imageNamed:@"bgBlack"];
    }
    return _backGroundImageView;
}

-(UIFont *)font{
    if (!_font) {
        _font = [UIFont systemFontOfSize:12];
    }
    return _font;
}

@end
